package com.wangzhenyu.learn.test.android_.fragmentViewModel

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.wangzhenyu.learn.databinding.TestFragmentRoomBinding
import com.wangzhenyu.common.base.BaseVBFragment
import com.wangzhenyu.common.util.log.LogUtil
 import com.wangzhenyu.learn.test.jetpack.room_.InstanceRoom
import com.wangzhenyu.learn.common.view.getViewText

class RoomTestFragment : BaseVBFragment<TestFragmentRoomBinding>() {


    private val viewModel: RoomTestFragmentViewModel by viewModels()

    override fun getBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = TestFragmentRoomBinding.inflate(inflater, container, false)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        viewModel.storeData.observe(viewLifecycleOwner)
        {
            for (i in it) {
                LogUtil.default01(i.toString())
            }
        }


        binding?.apply {

            bt01.setOnClickListener {
                val store = InstanceRoom.Store(input01.getViewText())
                viewModel.insertStore(store)
                val master = InstanceRoom.Master("WZY", input01.getViewText())
                viewModel.insertMaster(master)
            }

            bt02.setOnClickListener {
                val store1 = InstanceRoom.Store(input01.getViewText())
                val store2 = InstanceRoom.Store(input01.getViewText() + "gif")
                viewModel.insertStore(store1, store2)
                val master01 = InstanceRoom.Master("WZY", input01.getViewText())
                val master02 = InstanceRoom.Master("WZY", input01.getViewText() + "gif")
                viewModel.insertMaster(master01, master02)
            }

            bt03.setOnClickListener {
                val store1 = InstanceRoom.Store("更新01")
                store1.id = 1
                val store2 = InstanceRoom.Store("更新02")
                store2.id = 2
                viewModel.updateStore(store1, store2)
            }

            bt04.setOnClickListener {
                val store1 = InstanceRoom.Store("更新01")
                store1.id = 1
                val store2 = InstanceRoom.Store("更新02")
                store2.id = 2
                viewModel.deleteStore(store1, store2)
            }

            bt05.setOnClickListener {
                viewModel.storeName.value = input01.getViewText()
            }

            bt06.setOnClickListener {
                viewModel.queryStoreAndMaster(input01.getViewText())
            }

        }


    }
}